package common.entities.persistent;

import common.entities.abst.AbstPersistentEntity;

/**
 * @author Yoyo
 *
 */
public class User extends AbstPersistentEntity {

	private static int nameMinLength = 1;
	private static int nameMaxLength = 45;
	
	private static int passwordMinLength = 1;
	private static int passwordMaxLength = 45;
	
	private String name;
	private String password;
	private int gamesPlayed;
	private long moneyWon;
	private int personalRecord;
	
	/**
	 * This constructor will be used for entities that are fetched from the DB
	 * 
	 * @param id
	 * @param name
	 * @param password
	 * @param gamesPlayed - holds the number of games (full games) played by the user
	 * @param moneyWon - holds the sum of money the user won during his games
	 * @param highestWin - holds users personal the highest amount of money he won
	 */
	public User(int id, String name, String password, int gamesPlayed, long moneyWon, int personalRecord) {
		super(id);
		
		this.name = fixEscapeCharacters(name);
		this.password = fixEscapeCharacters(password);
		this.gamesPlayed = gamesPlayed;
		this.moneyWon = moneyWon;
		this.personalRecord = personalRecord;
	}
	
	/**
	 * This constructor will be used for new entities (i.e still weren't saved into DB)
	 * 
	 * @param name
	 * @param password
	 */
	public User(String name, String password) {
		super();
		this.name = fixEscapeCharacters(name);
		this.password = fixEscapeCharacters(password);
	}
	
	public User() {
		super();
		
		this.name = null;
		this.password = null;
	}
	
	public User(int id, String name, String password) {
		super(id);
		
		this.name = fixEscapeCharacters(name);
		this.password = fixEscapeCharacters(password);
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return fixEscapeCharactersReverse(name);
	}
	
	public String escapedGetName() {
		return name;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return fixEscapeCharactersReverse(password);
	}
	
	public String escapedGetPassword() {
		return password;
	}


	/**
	 * @return the gamesPlayed
	 */
	public int getGamesPlayed() {
		return gamesPlayed;
	}


	/**
	 * @return the moneyWon
	 */
	public long getMoneyWon() {
		return moneyWon;
	}

	/**
	 * sets the number of games played + 1
	 */
	public void addGamesPlayed(){
		gamesPlayed++;
	}

	/**
	 * sets the personalRecord
	 */
	public void setPersonalRecord(int newRecord) {
		 personalRecord = newRecord;;
	}
	
	/**
	 * @return the personalRecord
	 */
	public int getPersonalRecord() {
		return personalRecord;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = fixEscapeCharacters(name);
	
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = fixEscapeCharacters(password);
	}

	/**
	 * @param gamesPlayed the gamesPlayed to set
	 */
	public void setGamesPlayed(int gamesPlayed) {
		this.gamesPlayed = gamesPlayed;
	}

	/**
	 * @param moneyWon the moneyWon to set
	 */
	public void setMoneyWon(long moneyWon) {
		this.moneyWon = moneyWon;
	}

	/**
	 * Is used to validate the name according to the DB constraints
	 * 
	 * @param name
	 * @throws IllegalArgumentException
	 */
	public static void validateName(String name) throws IllegalArgumentException{
		if(name == null)
			throw new IllegalArgumentException("Name cannot be null");
		else if(name.length() < User.nameMinLength)
			throw new IllegalArgumentException(String.format("Name cannot have less than %s characters", User.nameMinLength));
		else if(name.length() > User.nameMaxLength)
			throw new IllegalArgumentException(String.format("Name cannot have more than %s characters", User.nameMaxLength));
	}
	
	/**
	 * Is used to validate the password according to the DB constraints
	 * 
	 * @param password
	 * @throws IllegalArgumentException
	 */
	public static void validatePassword(String password) throws IllegalArgumentException{
		if(password == null)
			throw new IllegalArgumentException("Password cannot be null");
		else if(password.length() < User.passwordMinLength)
			throw new IllegalArgumentException(String.format("Password cannot have less than %s characters", User.passwordMinLength));
		else if(password.length() > User.passwordMaxLength)
			throw new IllegalArgumentException(String.format("Password cannot have more than %s characters", User.passwordMaxLength));
	}
	


	
}
